// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   HKMarketPanel.java

package com.boci.clientapp.holding.ui.control;

import com.boci.clientapp.holding.action.HoldingAction;
import com.boci.clientapp.holding.constant.HoldingConst;
import com.boci.clientapp.holding.vo.HoldingDetailVO;
import com.boci.clientapp.holding.vo.HoldingVO;
import com.boci.clientapp.main.ClientAppLoader;
import com.boci.common.constant.ClientAppConst;
import com.boci.common.ui.control.*;
import com.boci.common.util.*;
import com.boci.common.vo.ResponseVO;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;

// Referenced classes of package com.boci.clientapp.holding.ui.control:
//            TradeRenderer, TradeEditor

public class HKMarketPanel extends JPanel
    implements TableModelListener
{

    public HKMarketPanel(Locale pLocale)
    {
        msgConf = ClientAppLoader.getMessagesConfigurator();
        initGUI(pLocale);
        setText(pLocale);
    }

    private void initGUI(Locale pLocale)
    {
        titlePanel = new JPanel();
        hkMarketSclPane = new JScrollPane();
        tblHKMarket = new JTable();
        tmHKMarket = new CMTableModel();
        tradeRenderer = new TradeRenderer(false, pLocale);
        tradeEditor = new TradeEditor(false, pLocale);
        nameRenderer = new NameRenderer(pLocale);
        ccyRenderer = new NameRenderer(pLocale);
        stringRenderer = new StringRenderer();
        numericRenderer = new StringRenderer();
        codeRenderer = new StringRenderer();
        ccyRenderer.setHorizontalAlignment(0);
        numericRenderer.setHorizontalAlignment(4);
        codeRenderer.setHorizontalAlignment(0);
        tblHKMarket.setModel(tmHKMarket);
        tblHKMarket.getTableHeader().setReorderingAllowed(false);
        tblHKMarket.setSelectionMode(1);
        tblHKMarket.setCellSelectionEnabled(false);
        tmHKMarket.addTableModelListener(this);
        tmHKMarket.setEditableCol(6);
        hkMarketSclPane.setViewportView(tblHKMarket);
        hkMarketSclPane.getViewport().setOpaque(false);
        hkMarketSclPane.setOpaque(false);
        hkMarketSclPane.setBorder(new EtchedBorder(1));
        lblHKMarketTitle = new JLabel();
        titlePanel.setLayout(new FlowLayout(0));
        titlePanel.setOpaque(false);
        titlePanel.add(lblHKMarketTitle);
        titlePanel.setPreferredSize(new Dimension(0, 25));
        setLayout(new BorderLayout());
        setOpaque(false);
        add(titlePanel, "North");
        add(hkMarketSclPane, "Center");
        setTableHeight();
    }

    private void setFont(Locale pLocale)
    {
        lblHKMarketTitle.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        tblHKMarket.getTableHeader().setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        stringRenderer.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        codeRenderer.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        numericRenderer.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
    }

    public void setText(Locale pLocale)
    {
        tmHKMarket.removeTableModelListener(this);
        tradeRenderer.setText(pLocale);
        tradeEditor.setText(pLocale);
        nameRenderer.setText(pLocale);
        ccyRenderer.setText(pLocale);
        lblHKMarketTitle.setText(msgConf.getUIMessages("HOLDING.HKMARKET", pLocale));
        setFont(pLocale);
        String strHKMarketColName[] = {
            msgConf.getUIMessages("HOLDING.COL_NAME", pLocale), msgConf.getUIMessages("HOLDING.COL_CODE", pLocale), msgConf.getUIMessages("HOLDING.COL_CCY", pLocale), msgConf.getUIMessages("HOLDING.COL_QTY", pLocale), msgConf.getUIMessages("HOLDING.COL_LAST_CLOSING_PRICE", pLocale), msgConf.getUIMessages("HOLDING.COL_PREVIOUS_VALUE", pLocale), msgConf.getUIMessages("HOLDING.COL_TRADE", pLocale)
        };
        tmHKMarket.setColumnName(strHKMarketColName);
        tmHKMarket.fireTableStructureChanged();
        for(int i = 0; i < tblHKMarket.getColumnCount(); i++)
        {
            TableColumn column = tblHKMarket.getColumnModel().getColumn(i);
            if(i == 0)
                column.setCellRenderer(nameRenderer);
            else
            if(i == 1)
                column.setCellRenderer(codeRenderer);
            else
            if(i == 2)
                column.setCellRenderer(ccyRenderer);
            else
            if(i == 3 || i == 4 || i == 5)
                column.setCellRenderer(numericRenderer);
            else
            if(i == 6)
            {
                column.setCellRenderer(tradeRenderer);
                column.setCellEditor(tradeEditor);
                column.setMinWidth(HoldingConst.COL_WIDTH_HOLDING_HKMARKET[i]);
                column.setMaxWidth(HoldingConst.COL_WIDTH_HOLDING_HKMARKET[i]);
            } else
            {
                column.setCellRenderer(stringRenderer);
            }
            column.setPreferredWidth(HoldingConst.COL_WIDTH_HOLDING_HKMARKET[i]);
        }

        tmHKMarket.addTableModelListener(this);
    }

    public void refresh(ResponseVO pResponseVO)
    {
        tmHKMarket.removeTableModelListener(this);
        if(pResponseVO == null)
        {
            Object strTableData[][] = new Object[0][7];
            tmHKMarket.setData(strTableData);
            tmHKMarket.fireTableDataChanged();
            hkMarketSclPane.getViewport().setViewPosition(new Point(0, 0));
            setTableHeight();
        } else
        if(pResponseVO instanceof HoldingVO)
        {
            HoldingVO holdingVO = (HoldingVO)pResponseVO;
            holdingDetailVOList = holdingVO.getHkStockList();
            Object strTableData[][] = new Object[holdingDetailVOList.size()][7];
            for(int i = 0; i < holdingDetailVOList.size(); i++)
            {
                HoldingDetailVO holdingDetailVO = (HoldingDetailVO)holdingDetailVOList.get(i);
                String strName[] = {
                    holdingDetailVO.getStockName(), holdingDetailVO.getStockNameChi(), holdingDetailVO.getStockNameChs()
                };
                String strCur[] = {
                    msgConf.getUIMessages(holdingDetailVO.getCurrency(), ClientAppConst.EN_US), msgConf.getUIMessages(holdingDetailVO.getCurrency(), ClientAppConst.ZH_HK), msgConf.getUIMessages(holdingDetailVO.getCurrency(), ClientAppConst.ZH_CN)
                };
                strTableData[i][0] = strName;
                strTableData[i][1] = holdingDetailVO.getSymbol();
                strTableData[i][2] = strCur;
                String strPrice = StringUtil.formatPriceValue(holdingDetailVO.getPrice());
                String strMktValue = StringUtil.formatPriceValue(holdingDetailVO.getMarketValue());
                String strQty = StringUtil.formatQtyValue(holdingDetailVO.getQuantity());
                strTableData[i][3] = strQty;
                strTableData[i][4] = strPrice;
                strTableData[i][5] = strMktValue;
                strTableData[i][6] = "";
            }

            tmHKMarket.setData(strTableData);
            tmHKMarket.fireTableDataChanged();
            hkMarketSclPane.getViewport().setViewPosition(new Point(0, 0));
            setTableHeight();
        }
        tmHKMarket.addTableModelListener(this);
    }

    public void tableChanged(TableModelEvent e)
    {
        String strActionType;
        int intSelectedCol = e.getColumn();
        int intSelectedRow = e.getFirstRow();
        strActionType = (String)tmHKMarket.getValueAt(intSelectedRow, intSelectedCol);
        if(strActionType == null || strActionType.length() == 0)
            return;
        if(!strActionType.equals("BUY") && !strActionType.equals("SELL"))
            return;
        HashMap detailVOMap;
        try{
	        PerformanceLogger.startAppTimer();
	        detailVOMap = getInputValue();
	        if(detailVOMap == null || detailVOMap.get("SELECTED_VO") == null)
	        {
	            PerformanceLogger.stopAppTimer();
	            PerformanceLogger.doLog((new StringBuilder()).append("HOLDING_SCREEN.").append(strActionType).toString());
	            return;
	        }
        }finally{
            PerformanceLogger.stopAppTimer();
            PerformanceLogger.doLog((new StringBuilder()).append("HOLDING_SCREEN.").append(strActionType).toString());        	
        }
        HoldingAction holdingAction = new HoldingAction();
        holdingAction.doProcess(detailVOMap, strActionType);
        tradeEditor.resetValue();

    }

    private HashMap getInputValue()
    {
        HashMap map = new HashMap();
        map.put("SELECTED_VO", getSelectedVO());
        return map;
    }

    private HoldingDetailVO getSelectedVO()
    {
        int intSelectedCol = tblHKMarket.getSelectedColumn();
        int intSelectedRow = tblHKMarket.getSelectedRow();
        if(holdingDetailVOList == null || holdingDetailVOList.size() < intSelectedRow)
            return null;
        if(intSelectedCol == 6)
        {
            HoldingDetailVO holdingDetailVO = (HoldingDetailVO)holdingDetailVOList.get(intSelectedRow);
            return holdingDetailVO;
        } else
        {
            return null;
        }
    }

    public void setTableHeight()
    {
        int intTableHeight = tblHKMarket.getRowHeight() * 6 + tblHKMarket.getTableHeader().getPreferredSize().height + 4;
        hkMarketSclPane.setPreferredSize(new Dimension(0, intTableHeight));
        hkMarketSclPane.setMinimumSize(new Dimension(0, intTableHeight));
    }

    private JPanel titlePanel;
    private JTable tblHKMarket;
    private CMTableModel tmHKMarket;
    private JScrollPane hkMarketSclPane;
    private JLabel lblHKMarketTitle;
    private TradeRenderer tradeRenderer;
    private TradeEditor tradeEditor;
    private NameRenderer nameRenderer;
    private NameRenderer ccyRenderer;
    private StringRenderer stringRenderer;
    private StringRenderer numericRenderer;
    private StringRenderer codeRenderer;
    private ArrayList holdingDetailVOList;
    private MessagesConfigurator msgConf;
}
